﻿using justice.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace justice.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        justiceEntities dal = new justiceEntities();
        public ActionResult Index()
        {
            ViewBag.mode = 0;
            return View();
        }

        /// <summary>
        /// 错误页面
        /// </summary>
        /// <returns></returns>
        [decide(IsCheck = false)]
        public ActionResult error()
        {
            return View();
        }

        /// <summary>
        /// 鉴定委托书视图页
        /// </summary>
        /// <returns></returns>
        public ActionResult ShroffBook()
        {
            List<nationality> na = dal.nationality.ToList();
            ViewBag.na = na;
            List<inserer> ins = dal.inserer.ToList();
            ViewBag.ins = ins;
            ViewBag.number = creates();
            return View();
        }

        /// <summary>
        /// 获取被鉴定人信息
        /// </summary>
        /// <param name="card"></param>
        /// <returns></returns>
        [decide(IsCheck = false)]
        public JsonResult getbe(string card)
        {
            be_appraiser be = dal.be_appraiser.ToList().Find(l=>l.ID_card == card);
            return Json(be);
        }


        /// <summary>
        /// 添加鉴定委托书
        /// </summary>
        /// <param name="shfbok"></param>
        /// <param name="beapp"></param>
        /// <param name="shroff_time_limit_1"></param>
        /// <param name="inform_insurer_1"></param>
        /// <param name="inform_insurer_2"></param>
        /// <param name="use_shroff_material"></param>
        /// <param name="special_situation"></param>
        /// <returns></returns>
        public ActionResult AddShroffBook(Shroff_book shfbok,be_appraiser beapp,string shroff_time_limit_1,string inform_insurer_1,string inform_insurer_2, List<string> use_shroff_material,List<string> special_situation)
        {
            be_appraiser bep = dal.be_appraiser.ToList().Find(l => l.ID_card == beapp.ID_card);
            if (bep == null)
            {
                dal.be_appraiser.Add(beapp);
                int be = dal.SaveChanges();
                if (be == 0)
                {
                    ModelState.AddModelError("BookError","Tips:提交失败，请重试。");
                    return View("ShroffBook");
                }
            }
            string use = "";
            string spec = "";
            foreach (var item in use_shroff_material)
            {
                use += item;
            }
            foreach (var item in special_situation)
            {
                spec += item;
            }
            
            string number = creates();

            shfbok.shroff_number = number;
            be_appraiser bepc = dal.be_appraiser.ToList().Find(l => l.ID_card == beapp.ID_card);
            shfbok.main_case = bepc.user_id;
            shfbok.use_shroff_material = use;
            shfbok.special_situation = spec;
            shfbok.shroff_time_limit += '*' + shroff_time_limit_1;
            shfbok.enter_user_id = Convert.ToInt32(Session["uid"]);
            if (shfbok.inform_insurer != null)
            {
                shfbok.inform_insurer += "*" + inform_insurer_1 + "*" + inform_insurer_2;
            }
            shfbok.consignation_transaction = shfbok.consignation_transaction.Replace("\r\n", "<br />");
            shfbok.shroff_material = shfbok.shroff_material.Replace("\r\n", "<br />");
            shfbok.other_transaction = shfbok.other_transaction.Replace("\r\n", "<br />");
            shfbok.remarks = shfbok.remarks.Replace("\r\n", "<br />");
            dal.Shroff_book.Add(shfbok);
            int sho = dal.SaveChanges();
            if (sho == 0)
            {
                ModelState.AddModelError("BookError", "Tips:提交失败，请重试。");
                dal.be_appraiser.Remove(bep);
            }
            else
            {
                Shroff_book sk = dal.Shroff_book.ToList().Find(l=>l.shroff_number == shfbok.shroff_number);
                MakingsCirculation mak = new MakingsCirculation();
                mak.ShroffId = sk.shroff_id;
                ForensicExaminationRecord fod = new ForensicExaminationRecord();
                fod.TableNumber = sk.shroff_id;
                ReceiptServiceEvaluations recip = new ReceiptServiceEvaluations();
                recip.ShroffId = sk.shroff_id;
                ForensicOpinion foi = new ForensicOpinion();
                foi.MainInfo = sk.shroff_id;
                ShroffTrust shrut = new ShroffTrust();
                shrut.shroffid = sk.shroff_id;
                dal.MakingsCirculation.Add(mak);
                dal.ForensicExaminationRecord.Add(fod);
                dal.ReceiptServiceEvaluations.Add(recip);
                dal.ForensicOpinion.Add(foi);
                dal.ShroffTrust.Add(shrut);
                int a = dal.SaveChanges();
                if (a > 0)
                {
                    Session["success"] = sk.shroff_id;
                    return new RedirectResult("/shroffbook/shroffbookinfos/"+sk.shroff_id);
                }
            }
            
            return View("ShroffBook");
        }

        /// <summary>
        /// 从session中移除success
        /// </summary>
        [decide(IsCheck = false)]
        public void clearsession()
        {
            Session.Remove("success");
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [MyAuthorize(IsCheck = false)]
        [decide(IsCheck = false)]
        public ActionResult Out()
        {
            Session.Clear();
            return new RedirectResult("/Admin/LoginIndex");
        }

        /// <summary>
        /// 生成编号
        /// </summary>
        /// <returns></returns>
        public string creates()
        {
            justice_user user = dal.justice_user.ToList().Find(l => l.id == Convert.ToInt32(Session["uid"]));
            judiciary jud = dal.judiciary.ToList().Find(l => l.id == user.unit_id);
            Shroff_book shok = dal.Shroff_book.ToList().Find(o => o.shroff_id == dal.Shroff_book.ToList().Max(l => l.shroff_id));
            string maxid = "";
            if (shok == null)
            {
                maxid = "001";
            }
            else
            {
                maxid = (shok.shroff_id + 1).ToString().PadLeft(3, '0');
            }
            string number = jud.abbreviation + "-SJ-" + DateTime.Now.Year + "-" + DateTime.Now.ToString("MM") + "-" + maxid;
            return number;
        }
    }
}